package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Auther:
 * @Description:
 * @Date: 2019/11/19 15:46
 */
@RestController
@RequestMapping("/consumer")
public class DeptController_Consumer {

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";

    /**
     * 使用 restTemplate 访问 restful 接口，
     *  (url， requestMap， ResponseBean.class)
     *  参数： rest请求地址，请求参数，HTTP 相应转换被转换成的对象类型
     * */
    @Autowired
    @Qualifier("myRestTemplate")
    private RestTemplate restTemplate;

//    @PostMapping("/dept/add")
    @RequestMapping("/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/"+id, Dept.class);
    }

    @GetMapping("/dept/list")
    public List<Dept> list(){
        System.out.println("dept/list");
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    }
}
